var express = require('express');
var router = express.Router();
var User =  require('../models/user');

/* GET home page. */
router.get('/', function(req, res, next) {
    res.send('Express,Very Goood');
});

// 登录
router.post('/login', function(req, res, next) {
    let param = {
        userName: req.body.userName,
        userPwd: req.body.userPwd
    }

    User.findOne(param, function(err, doc) {
        if (err) {
            res.json({
                status: '1',
                msg: err.message
            });
        } else {
            if (doc) {
                res.cookie('userId', doc.userId, {
                    path: '/',
                    maxAge: 1000*60*60
                });
                res.cookie('userName', doc.userName, {
                    path: '/',
                    maxAge: 1000*60*60
                });
                res.json({
                    status: '0',
                    msg: '',
                    result: '登录成功'
                });
            } else {
                res.json({
                    status: '20001',
                    msg: '无此用户信息',
                })
            }
        }
    });
});

// 退出登录
router.post('/loginOut', function(req, res, next) {
    res.cookie('userId', '',{
        path: '/',
        maxAge: 0
    });
    res.cookie('userName', '',{
        path: '/',
        maxAge: 0
    });
    res.json({
        status: '0',
        msg: '',
        result: '退出成功'
    })
});
module.exports = router;
